package cn.bdqn.controller;

import cn.bdqn.domain.Receipt;
import cn.bdqn.service.ReceiptService;
import cn.bdqn.utils.result.Page;
import cn.bdqn.utils.result.Result;
import cn.bdqn.utils.result.ResultGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *回执单控制层
 * @author dkj
 */
@RestController
public class ReceiptController {


    /**
     * 创建日志记录器
     */
    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ReceiptService receiptService;

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/receipt/query/{id}")
    public Result queryByPrimaryKey(@PathVariable("id") Integer id){
        try {
            logger.info("进入了方法，参数：id={}",id);
            //根据id查询
            Receipt receipt = receiptService.queryByPrimaryKey(id);
            //返回结果
            return ResultGenerator.genSuccessResult(receipt);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("报错了："+e.getMessage());
            return ResultGenerator.genFailResult("查询失败");
        }
    }


    /**
     * 根据发货单号模糊查询
     * @param sendFreightNumber
     * @param state
     * @return
     */
    @GetMapping("/receipt/query/{currentPage}/{pageSize}/{sendFreightNumber}/{state}")
    public Result queryBySendFreightId(@PathVariable("currentPage") Integer currentPage,
                                       @PathVariable("pageSize") Integer pageSize,
                                       @PathVariable("sendFreightNumber") String sendFreightNumber,
                                       @PathVariable("state") Integer state){
        try {

            logger.info("进入了方法，参数：currentPage={},pageSize={},sendFreightNumber{},state",currentPage,pageSize,sendFreightNumber,state);
            //查询结果
            Page page = receiptService.queryBySendFreightId(currentPage, pageSize, sendFreightNumber, state);
            //返回结果
            return ResultGenerator.genSuccessListResult(page.getMenuList(),page.getTotalRecord());
        }catch (Exception e){
            e.printStackTrace();
            logger.error("报错了："+e.getMessage());
            return ResultGenerator.genFailResult("查询失败");
        }
    }


    /**
     * 新增
     * @param receipt
     * @return
     */
    @PostMapping("/receipt/save/")
    public Result saveReceipt(@RequestBody Receipt receipt){
        try {
            logger.info("进入了方法，参数：receipt={}",receipt);
            //新增
            Receipt receipt1 = receiptService.saveReceipt(receipt);
            //返回结果
            return ResultGenerator.genSuccessResult(receipt1);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("报错了："+e.getMessage());
            return ResultGenerator.genFailResult("新增失败");
        }
    }


    /**
     * 修改当前对象
     * @param receipt
     */
    @PutMapping("/receipt/update/")
    public Result updateReceipt(@RequestBody Receipt receipt){

        try {
            logger.info("进入了方法，参数：receipt={}",receipt);
            //修改
            receiptService.updateReceipt(receipt);
            //返回结果
            return ResultGenerator.genSuccessResult();
        }catch (Exception e){
            e.printStackTrace();
            logger.error("报错了："+e.getMessage());
            return ResultGenerator.genFailResult("修改失败");
        }
    }

}
